<template>
  <view
    class="order-item"
    :class="{ active: active }"
    @click="$emit('click')"
  >
    <view class="header">
      <v-icon
        size="36rpx"
        src="@/static/icons/cart-merchant.svg"
      />
      <view class="merchant-name">
        <view class="name-text">
          {{ order.merchant_name }}
        </view>
      </view>
      <view
        v-if="complaintStatus"
        class="status"
        :class="{
          highlight:
            complaintStatus === '您已投诉，待商家处理' ||
            complaintStatus === '商家已回复'
        }"
      >
        {{ complaintStatus }}
      </view>
      <view
        v-if="aftersalesStatus"
        class="status highlight"
      >
        {{ aftersalesStatus }}
      </view>
    </view>
    <complaint-product :product="product" />
    <view
      v-if="info"
      class="info"
    >
      {{ info }}
    </view>
    <view class="summary">
      <view class="summary-label">
        实付
      </view>
      <v-price
        :price="price"
        size="32rpx"
        color="#333333"
      />
    </view>
  </view>
</template>

<script setup lang="ts">
import ComplaintProduct from './complaint-product.vue';

const props = defineProps<{
  order: OrderVO;
  complaint?: OrderComplaintDetailVO;
  aftersales?: BackRefundViewVO;
  product: OrderDetailVO;
  active?: boolean;
  info?: string;
}>();

defineEmits<{
  click: [];
}>();

const complaint = toRef(props, 'complaint');
const complaintStatus = computed(() => {
  if (!complaint.value) return undefined;
  switch (true) {
    case complaint.value.status === 'PENDING':
      return '您已投诉，待商家处理';
    case complaint.value.status === 'PROCESSING':
      return '商家已回复';
    case complaint.value.status === 'PROCESSED':
      return '商家已处理';
    case complaint.value.status === 'FINISH':
      return '投诉已完成';
    case complaint.value.status === 'CLOSE':
      return '投诉已关闭';
    case complaint.value.status === 'REVOCATION':
      return '投诉已撤回';
    default:
      return '';
  }
});
const aftersales = toRef(props, 'aftersales');
const aftersalesStatus = computed(() => {
  if (!aftersales.value) return undefined;
  switch (true) {
    case aftersales.value.basic_status === 'PROCEED_REFUND':
      return '售后中';
    case aftersales.value.basic_status === 'SUCCESS':
      return '售后完成';
    default:
      return '';
  }
});

const price = computed(() => {
  const price = props.product.sell_price;
  const count = props.product.quantity;
  const discount = props.product.discount_price ?? 0;
  const val = price * count - discount;
  return Math.max(val, 0);
});
</script>

<style lang="scss" scoped>
.order-item {
  padding: calc(28rpx - 2px) calc(24rpx - 2px);
  margin: 0 24rpx 24rpx;
  background-color: white;
  border-radius: 16rpx;
  border: 1px solid white;
  display: flex;
  flex-direction: column;
  gap: 28rpx;
  &.active {
    border: 1px solid #ff2f3b;
  }
  .header {
    height: 40rpx;
    display: flex;
    align-items: center;
    gap: 20rpx;
    .merchant-name {
      flex: 1;
      min-width: 0;
      display: flex;
      align-items: center;
      .name-text {
        font-size: 28rpx;
        font-weight: bold;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        word-break: break-all;
      }
    }
    .status {
      line-height: 36rpx;
      color: #999999;
      &.highlight {
        color: #ff2f3b;
      }
    }
  }

  .product {
    .product-content {
      display: flex;
      align-items: stretch;
      gap: 24rpx;
      .product-info {
        flex: 1;
        min-width: 0;
        display: flex;
        flex-direction: column;
        .main-info {
          display: flex;
          align-items: center;
          margin-bottom: 12rpx;
          gap: 8rpx;
          .name {
            font-weight: bold;
            flex: 1;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }
          .price {
            width: fit-content;
            flex-shrink: 0;
          }
        }
        .sub-info {
          display: flex;
          align-items: flex-start;
          gap: 8rpx;
          .spec-info {
            flex: 1;
            font-size: 24rpx;
            line-height: 32rpx;
            color: #666666;
          }
          .quantity {
            flex-shrink: 0;
            line-height: 32rpx;
            color: #999999;
          }
        }
      }
    }
  }
  .info {
    background-color: #f5f6fa;
    border-radius: 4rpx;
    line-height: 60rpx;
    padding: 0 24rpx;
    font-weight: bold;
  }
  .summary {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8rpx;
    .summary-label {
      font-size: 28rpx;
    }
  }
  .footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 30rpx;
    .aftersales-status {
      margin-right: auto;
      color: #ff2f3b;
      padding: 12rpx 0;
    }
  }
}
</style>
